package com.yunyan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.entity.UserData;
import com.yunyan.mapper.UserMapper;
import com.yunyan.service.UserService;
import com.yunyan.util.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserData> implements UserService {

    @Override
    public boolean register(UserData userData) {
        UserData list = this.getOne(new LambdaQueryWrapper<UserData>()
                .eq(UserData::getAccount, userData.getAccount())
        );
        if (list == null) {
            userData.setCreateTime(new Date());
            this.save(userData);
            return true;
        }
        return false;
    }

    @Override
    public Object login(UserData userData) {
        UserData item = this.getOne(new LambdaQueryWrapper<UserData>()
                .eq(UserData::getAccount, userData.getAccount())
        );
        if (item != null) {
            if (item.getPassword().equals(userData.getPassword())) {
                Map<String, Object> res = new HashMap<>();
                res.put("token", JwtUtil.createToken(item.getId()));
                res.put("user", item);
                return res;
            }
            return null;
        } else {
            return null;
        }
    }
}
